package com.block.source.service.impl;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.block.common.service.IAddressInfoService;
import com.block.common.service.IApplicationInfoService;
import com.block.common.service.LogFactory;
import com.block.db.entity.*;
import com.block.db.mapper.MemberExtInfoMapper;
import com.block.db.mapper.MemberInfoMapper;
import com.block.db.mapper.SourceRecordInfoMapper;
import com.block.source.service.ISourceRecordInfoService;
import com.block.test.reqbean.source.CheckUserInfo;
import com.block.test.reqbean.source.ImportData;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.support.TransactionTemplate;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDate;

/**
 * <p>
 * 数据来源记录 服务实现类
 * </p>
 *
 * @author 
 * @since 2023-05-22
 */
@Service
public class SourceRecordInfoServiceImpl extends ServiceImpl<SourceRecordInfoMapper, SourceRecordInfo> implements ISourceRecordInfoService {


    private static Logger log = LogFactory.getSourceLog();

    @Resource
    IAddressInfoService addressInfoService;
    @Resource
    MemberInfoMapper memberInfoMapper;
    @Resource
    MemberExtInfoMapper memberExtInfoMapper;
    @Resource
    IApplicationInfoService applicationInfoService;
    @Autowired
    TransactionTemplate transactionTemplate;

    @Override
    public SourceRecordInfo recordDataInfo(SourceTaskInfo taskInfo, CheckUserInfo checkUserInfo) {

        String dateInfo = LocalDate.now().toString();

        SourceRecordInfo newEntity = new SourceRecordInfo();
        newEntity.setDateInfo(dateInfo);
        newEntity.setInfoId(taskInfo.getSourceId());
        newEntity.setOutOrderNo(checkUserInfo.getOrderNo());
        newEntity.setMobileMd5(checkUserInfo.getMobileMd5());
        newEntity.setAge(checkUserInfo.getAge());
        newEntity.setSex(checkUserInfo.getSex());
        newEntity.setCityCode(checkUserInfo.getCityCode());
        newEntity.setReqIp(checkUserInfo.getReqIp());
        newEntity.setZhima(checkUserInfo.getZhima());
        newEntity.setBaitiao(checkUserInfo.getBaitiao());
        newEntity.setHuabei(checkUserInfo.getHuabei());
        newEntity.setLoanAmount(checkUserInfo.getLoamAmount());
        newEntity.setXueli(checkUserInfo.getXueli());
        newEntity.setHouse(checkUserInfo.getHouse());
        newEntity.setCar(checkUserInfo.getCar());
        newEntity.setGjj(checkUserInfo.getGjj());
        newEntity.setYuqi(checkUserInfo.getYuqi());
        newEntity.setShebao(checkUserInfo.getShebao());
        newEntity.setBaodan(checkUserInfo.getBaodan());
        newEntity.setEnterpriseOwner(checkUserInfo.getEnterpriseOwner());

        BigDecimal priceAmount = NumberUtil.mul(taskInfo.getBaseAmount(), BigDecimal.ONE.add(taskInfo.getAmountRate()));
        newEntity.setPriceAmount(priceAmount);
        newEntity.setBaseAmount(taskInfo.getBaseAmount());

        // 城市信息
        AddressInfo addressInfo = addressInfoService.getAddressInfoByCode(checkUserInfo.getCityCode());
        if (ObjectUtil.isEmpty(addressInfo)){
            log.info("【入库资质信息】城市信息为空【cityCode】{}", checkUserInfo.getCityCode());
            return null;
        }
        newEntity.setProvince(addressInfo.getParentName());
        newEntity.setProvinceCode(addressInfo.getParentCode());
        newEntity.setCity(addressInfo.getName());
        newEntity.setCityCode(addressInfo.getCodeValue());

        boolean save = this.save(newEntity);
        if (save){
            return this.getById(newEntity.getId());
        }
        return null;
    }

    @Override
    public MemberExtInfo getExtInfoByRecordInfo(SourceRecordInfo recordInfo) {

        MemberExtInfo memberExtInfo = new MemberExtInfo();
        memberExtInfo.setUid(recordInfo.getId());
        memberExtInfo.setAge(Integer.valueOf(recordInfo.getAge()));
        memberExtInfo.setSex(Integer.valueOf(recordInfo.getSex()));

        memberExtInfo.setLoanAmount(recordInfo.getLoanAmount());
        memberExtInfo.setZhimaScore(recordInfo.getZhima());
        memberExtInfo.setBaitiao(recordInfo.getBaitiao());
        memberExtInfo.setHuabei(recordInfo.getHuabei());
        memberExtInfo.setXueli(recordInfo.getXueli());

        memberExtInfo.setHouse(recordInfo.getHouse());
        memberExtInfo.setCar(recordInfo.getCar());
        memberExtInfo.setGjj(recordInfo.getGjj());
        memberExtInfo.setSocialInsurance(recordInfo.getShebao());
        memberExtInfo.setInsurance(recordInfo.getBaodan());
        memberExtInfo.setYuqi(recordInfo.getYuqi());
        memberExtInfo.setEnterpriseOwner(recordInfo.getEnterpriseOwner());

        memberExtInfo.setProvince(recordInfo.getProvince());
        memberExtInfo.setProvinceCode(recordInfo.getProvinceCode());
        memberExtInfo.setCity(recordInfo.getCity());
        memberExtInfo.setCityCode(recordInfo.getCityCode());

        return memberExtInfo;
    }

    @Override
    public boolean updateRecordInfo(SourceRecordInfo recordInfo, ImportData importData) {

        String realName = StrUtil.replace(importData.getRealName(), "先生", "").replace("女士", "");
        SourceRecordInfo updateEntity = new SourceRecordInfo();
        updateEntity.setId(recordInfo.getId());
        updateEntity.setMobile(importData.getMobile());
        updateEntity.setRealName(realName);
        updateEntity.setIdCard(importData.getIdCard());
        return this.updateById(updateEntity);
    }

    @Override
    public MemberExtInfo saveNewMemExtInfo(SourceBaseInfo sourceBaseInfo, Long recordId) {

        Long uid = transactionTemplate.execute(status -> {

            SourceRecordInfo recordInfo = this.getById(recordId);
            ApplicationInfo applicationInfo = applicationInfoService.getAppInfoByType("renxinhua");

            MemberInfo memberInfo = this.saveMemberInfo(sourceBaseInfo, recordInfo, applicationInfo);
            if (ObjectUtil.isEmpty(memberInfo)){
                return null;
            }

            MemberExtInfo memberExtInfo = this.saveMemberExtInfo(memberInfo, recordInfo);
            if (ObjectUtil.isEmpty(memberExtInfo)){
                status.setRollbackOnly();
                return null;
            }

            SourceRecordInfo updateEntity = new SourceRecordInfo();
            updateEntity.setId(recordInfo.getId());
            updateEntity.setUid(String.valueOf(memberExtInfo.getUid()));
            updateEntity.setMessage("用户信息初始化成功");
            this.updateById(updateEntity);

            return memberInfo.getUid();
        });

        if (ObjectUtil.isNotEmpty(uid)){
            return memberExtInfoMapper.selectById(uid);
        }
        return null;
    }

    public MemberExtInfo saveMemberExtInfo(MemberInfo memberInfo, SourceRecordInfo recordInfo){
        String dateInfo = LocalDate.now().toString();

        MemberExtInfo  memberExtInfo = new MemberExtInfo();
        memberExtInfo.setUid(memberInfo.getUid());
        memberExtInfo.setMobile(recordInfo.getMobile());
        memberExtInfo.setUsername(recordInfo.getRealName());
        memberExtInfo.setIdCard(recordInfo.getIdCard());
        memberExtInfo.setDateInfo(dateInfo);
        memberExtInfo.setAge(Integer.valueOf(recordInfo.getAge()));
        memberExtInfo.setSex(Integer.valueOf(recordInfo.getSex()));

        memberExtInfo.setAppCode(memberInfo.getAppCode());
        memberExtInfo.setAppName(memberInfo.getAppName());
        memberExtInfo.setAppType(memberInfo.getAppType());

        memberExtInfo.setChannelCode(memberInfo.getChannelCode());
        memberExtInfo.setChannelName(memberInfo.getChannelName());

        memberExtInfo.setProvince(recordInfo.getProvince());
        memberExtInfo.setProvinceCode(recordInfo.getProvinceCode());
        memberExtInfo.setCity(recordInfo.getCity());
        memberExtInfo.setCityCode(recordInfo.getCityCode());

        memberExtInfo.setLoanAmount(recordInfo.getLoanAmount());
        memberExtInfo.setZhimaScore(recordInfo.getZhima());
        memberExtInfo.setBaitiao(recordInfo.getBaitiao());
        memberExtInfo.setHuabei(recordInfo.getHuabei());
        memberExtInfo.setXueli(recordInfo.getXueli());

        memberExtInfo.setHouse(recordInfo.getHouse());
        memberExtInfo.setCar(recordInfo.getCar());
        memberExtInfo.setGjj(recordInfo.getGjj());
        memberExtInfo.setYuqi(recordInfo.getYuqi());
        memberExtInfo.setSocialInsurance(recordInfo.getShebao());
        memberExtInfo.setInsurance(recordInfo.getBaodan());
        memberExtInfo.setEnterpriseOwner(recordInfo.getEnterpriseOwner());

        int insert1 = memberExtInfoMapper.insert(memberExtInfo);
        if (insert1 < 1){
            return null;
        }
        return memberExtInfoMapper.selectById(memberInfo.getUid());
    }

    public MemberInfo saveMemberInfo(SourceBaseInfo sourceBaseInfo, SourceRecordInfo recordInfo, ApplicationInfo applicationInfo){

        String dateInfo = LocalDate.now().toString();
        MemberInfo memberInfo = new MemberInfo();
        memberInfo.setAppCode(applicationInfo.getAppCode());
        memberInfo.setAppName(applicationInfo.getAppName());
        memberInfo.setAppType(applicationInfo.getAppType());
        memberInfo.setExtInfoFlag(1);

        memberInfo.setDateInfo(dateInfo);
        memberInfo.setMobileMd5(recordInfo.getMobileMd5());
        memberInfo.setMobile(recordInfo.getMobile());
        memberInfo.setUsername(recordInfo.getRealName());
        memberInfo.setIdCard(recordInfo.getIdCard());

        memberInfo.setChannelCode(sourceBaseInfo.getChannelCode());
        memberInfo.setChannelName(sourceBaseInfo.getChannelName());

        memberInfo.setReqIp(recordInfo.getReqIp());

        int insert = memberInfoMapper.insert(memberInfo);
        if (insert < 1){
            return null;
        }
        return memberInfo;
    }

}
